/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package mantechhelpdesk.bean;

import java.io.IOException;
import java.sql.Connection;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.context.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import mantechhelpdesk.dao.DbConnection;
import mantechhelpdesk.dao.UsersDao;
import mantechhelpdesk.entity.Users;

/**
 *
 * @author DinhThang
 */
public class Login {

    /**
     * Creates a new instance of Login
     */
    private UsersDao usersDao = new UsersDao();
    private int userid = 0;
    private String username = "";
    private String password = "";
    private int role = 0;

    public int getUserid() {
        return userid;
    }

    public void setUserid(int userid) {
        this.userid = userid;
    }
    
    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public int getRole() {
        loadInfo();
        return role;
    }

    public void setRole(int role) {
        this.role = role;
    }

    public String checkLogin() {
        boolean login = false;
        FacesContext ct = FacesContext.getCurrentInstance();
        try {
            if (username.length() > 0 && password.length() > 0) {
                Connection con = DbConnection.getConnection();
                Users users = new Users();
                users.setUsername(username);
                users.setPassword(password);
                users = usersDao.checkLogin(con, users);
                if (users.getUserID() > 0) {
                    HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
                    HttpSession session = request.getSession(true);
                    session.setAttribute("HelpDeskLogin", users.getUserID());
                    login = true;
                }
                ct.addMessage("", new FacesMessage("Username or password incorrect!"));
            } else {
                ct.addMessage("", new FacesMessage("Must enter the username and password!"));
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            ct.addMessage("", new FacesMessage("Username or password incorrect!"));
        }

        if (login == true) {
            return "loginTrue";
        }
        return "loginFales";
    }

    public Login() {
        loadInfo();
    }

    private void loadInfo() {
        try {
            HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
            HttpSession session = request.getSession();
            Object tmp = session.getAttribute("HelpDeskLogin");
            int user_id = 0;
            if (null != tmp) {
                user_id = Integer.parseInt(tmp.toString());
            }
            if (user_id > 0) {
                Connection con = DbConnection.getConnection();
                Users users = usersDao.getObject(con, user_id);
                if (users.getUserID() > 0) {
                    role = users.getRoleID();
                    username = users.getUsername();
                    userid = users.getUserID();
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    public String logout() {
        HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
        HttpSession session = request.getSession(true);
        session.removeAttribute("HelpDeskLogin");
        try {
            ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
            externalContext.redirect("/MantechHelpdesk/faces/login.jsp");
        } catch (IOException ex) {
            Logger.getLogger(Login.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "logout";
    }
}
